package org.jeecg.modules.system.announcement.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.dto.MemberDTO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.system.announcement.entity.BikeAnnouncement;
import org.jeecg.modules.system.announcement.mapper.BikeAnnouncementMapper;
import org.jeecg.modules.system.announcement.service.IBikeAnnouncementService;
import org.jeecg.modules.system.announcement.vo.BikeAnnouncementRsp;
import org.jeecg.modules.system.announcement.vo.BikeAnnouncementSendReq;
import org.jeecg.modules.system.announcement.vo.BikeSysMsgReq;
import org.jeecg.modules.system.send.entity.BikeAnnouncementSend;
import org.jeecg.modules.utils.RandomCodeUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 消息管理
 * @Author: 月月鸟
 * @Date:   2021-06-15
 * @Version: V1.0
 */
@Service
public class BikeAnnouncementServiceImpl extends ServiceImpl<BikeAnnouncementMapper, BikeAnnouncement> implements IBikeAnnouncementService {
    @Resource
    private BikeAnnouncementMapper bikeAnnouncementMapper;
    @Override
    public Result<?> releaseOrRevoke(String id) {
        BikeAnnouncement bikeAnnouncement = bikeAnnouncementMapper.selectById(id);
        //消息状态 是 未发布 或 撤回   SendStatus 0未发布 1发布 2撤回
        if (bikeAnnouncement.getSendStatus().equals("0")||bikeAnnouncement.getSendStatus().equals("3")){
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            //发布人
            bikeAnnouncement.setSender(sysUser.getRealname());
            //发布时间
            bikeAnnouncement.setSendTime(new Date());
            //消息状态
            bikeAnnouncement.setSendStatus("1");
            bikeAnnouncementMapper.updateById(bikeAnnouncement);
            return Result.OK("发布成功");
        }else {
            //消息状态
            bikeAnnouncement.setSendStatus("2");
            bikeAnnouncementMapper.updateById(bikeAnnouncement);
            return Result.OK("撤回成功");
        }
    }

    @Override
    public Result<?> removeNewsById(String id) {
        BikeAnnouncement bikeAnnouncement = bikeAnnouncementMapper.selectById(id);
        if (bikeAnnouncement.getSendStatus().equals("1")){
            return Result.error("发布状态无法删除");
        }
        bikeAnnouncementMapper.deleteById(id);
        return Result.OK("已删除消息");
    }

    @Override
    public String checkCountByType(String id) {
        Integer selectCount = bikeAnnouncementMapper.selectCount(new QueryWrapper<BikeAnnouncement>()
                .ne("id", id)
                .eq("msg_category", "7")
                .eq("send_status", BikeConstant.y_n_yes));
        if (selectCount<3){
            return BikeConstant.y_n_yes;
        }
        return "滚屏公告最多发布3条";
    }

    @Override
    public String addCheckByType() {
        Integer selectCount = bikeAnnouncementMapper.selectCount(new QueryWrapper<BikeAnnouncement>()
                .eq("msg_category", "7")
                .eq("send_status", BikeConstant.y_n_yes));
        if (selectCount<3){
            return BikeConstant.y_n_yes;
        }
        return "滚屏公告最多发布3条";
    }

    @Override
    public List<BikeAnnouncementRsp> getSysMsgByUser(BikeSysMsgReq bikeSysMsgReq) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //个人系统消息
        bikeSysMsgReq.setUserId(sysUser.getId());
        if (StringUtils.isNotEmpty(bikeSysMsgReq.getType())){
            if (bikeSysMsgReq.getType().equals("1")){
                bikeSysMsgReq.setTitile(BikeConstant.sys_msg_report);
            }else if (bikeSysMsgReq.getType().equals("2")){
                bikeSysMsgReq.setTitile(BikeConstant.sys_msg_appeal);
            }else if (bikeSysMsgReq.getType().equals("3")){
                bikeSysMsgReq.setTitile(BikeConstant.sys_msg_opinion);
            }
        }
        return bikeAnnouncementMapper.getSysMsgByUser(bikeSysMsgReq);
    }

    @Override
    public Result<?> getSysMsgNoReadCount() {
        JSONObject data = new JSONObject();
        //上报未转工单数量
        data.put("report",bikeAnnouncementMapper.getReportCount());
        //诉求管理未处理数量
        data.put("appeal",bikeAnnouncementMapper.getAppealCount());
        return Result.OK(data);
    }

    @Override
    public Result<?> readMsgById(String id) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        bikeAnnouncementMapper.readMsgById(id,sysUser.getUsername());
        return Result.OK() ;
    }

    @Override
    public void addSysMsgByUser(List<String> userIds, String msg,String sender,String titile,String description) {
        //新增消息内容
        BikeAnnouncement bikeAnnouncement = new BikeAnnouncement();
        bikeAnnouncement.setId(RandomCodeUtils.getUUID());
        bikeAnnouncement.setTitile(titile);
        bikeAnnouncement.setDescription(description);
        bikeAnnouncement.setMsgType("USER");
        bikeAnnouncement.setMsgCategory("2");
        bikeAnnouncement.setSendStatus("1");
        bikeAnnouncement.setMsgContent(msg);
        bikeAnnouncement.setSendTime(new Date());
        bikeAnnouncement.setSender(sender);
        bikeAnnouncementMapper.insert(bikeAnnouncement);
        //批量导入关联信息
        BikeAnnouncementSendReq bikeAnnouncementSendReq = new BikeAnnouncementSendReq();
        bikeAnnouncementSendReq.setAnntId(bikeAnnouncement.getId());
            bikeAnnouncementSendReq.setReadFlag("0");
        for (String userId : userIds){
            bikeAnnouncementSendReq.setId(RandomCodeUtils.getUUID());
            bikeAnnouncementSendReq.setUserId(userId);
            bikeAnnouncementMapper.addSysMsgByUser(bikeAnnouncementSendReq);
        }
    }

    @Override
    public List<String> getSysUserIds(String permissionName) {
        return bikeAnnouncementMapper.getSysUserIds(permissionName);
    }
}
